package io.itit.ecp.admin.provide.filter;

import com.alibaba.fastjson.serializer.ValueFilter;
import io.itit.ecp.admin.provide.selected.CarbonSinkSelected;
import io.itit.grass.common.provide.action.resp.SelectedResp;
import io.itit.grass.common.server.helper.BeanHelper;
import io.itit.grass.common.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：碳汇数据键值转换
 * @Date：Create by 2023-12-01 10:45:08
 */
public interface ICarbonSinkDataFilter {

    String TYPE = "type";

//    String CARBON_SINK_DATA_TYPE = "ecp-admin.carbonSinkData.type";

    class CarbonSinkDataExportFilter implements ValueFilter {

        private final Map<String, String> typeMap;

        public CarbonSinkDataExportFilter() {
            typeMap = BeanHelper.i().getBean(CarbonSinkSelected.class).get(null)
                    .stream().collect(Collectors.toMap(SelectedResp::getValue, SelectedResp::getLabel));
        }

        @Override
        public Object process(Object object, String name, Object value) {
            if (StringUtils.equals(name, TYPE)) {
                return CommonUtils.orElse(typeMap.get(CommonUtils.toString(value)), value);
            }
            return value;
        }

    }

    class CarbonSinkDataUploadFilter implements ValueFilter {

        private final Map<String, String> typeMap;

        public CarbonSinkDataUploadFilter() {
            typeMap = BeanHelper.i().getBean(CarbonSinkSelected.class).get(null)
                    .stream().collect(Collectors.toMap(SelectedResp::getValue, SelectedResp::getLabel));
        }

        @Override
        public Object process(Object object, String name, Object value) {
            if (StringUtils.equals(name, TYPE)) {
                return CommonUtils.orElse(typeMap.get(CommonUtils.toString(value)), value);
            }
            return value;
        }

    }

}